package views;
/**
 * EDCManagerViewer
 * Viewer for EDCDirChooserPanel
 */
import io.XMLControler;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import ui.EDC;
import ui.EDCDirChooserPanel;
import utils.EDCResource;

public class EDCManagerViewer extends EDCDefaultViewer
implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private EDCDirChooserPanel dirChooserPane;
	private EDCDirChooserPanel catChooserPane;
	
	private JLabel lbCat;
	private JTextField txtCat;
//	private JButton btCat;
	
	private JButton btCancel;
	private JButton btOk;
	
	private File f = new File(EDCResource.URL_CATALOG_PATH);

	public EDCManagerViewer() {
		
		setTitle("Preferences");
		getContentPane().setLayout(new BorderLayout());

		dirChooserPane = new EDCDirChooserPanel("Folder name: ",
				"Choose Directory");
		dirChooserPane.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("DTD Folder Preferences"),
				BorderFactory.createEmptyBorder(5,5,5,5)));
		dirChooserPane.setPreferredSize(new Dimension(750, 100));
//		dirChooserPane.getTxtFolderName().setText(EDCResource.dtdFolderPath);
		try {
			dirChooserPane.getTxtFolderName().setText(EDCResource.inputFile.getCanonicalPath());
		} catch (Exception e) {
			e.printStackTrace();
		}
		getContentPane().add(dirChooserPane, BorderLayout.NORTH);
		
		JPanel catalogPane = new JPanel();
		catalogPane.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("DTD Catalog File Preferences"),
				BorderFactory.createEmptyBorder(5,5,5,5)));
		catalogPane.setPreferredSize(new Dimension(700, 100));
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		catalogPane.setLayout(gridbag);

		catalogPane.add(new JSeparator(SwingConstants.VERTICAL));
		catalogPane.add(Box.createHorizontalStrut(5));
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		
		lbCat = new JLabel("Catalog File name (.xml): ");
		catalogPane.add(lbCat, c);
		
		c.gridx = 0;
		c.gridy = 1;
		
		txtCat = new JTextField(20);
		txtCat.addActionListener(this);
		txtCat.setText(f.getName());
		catalogPane.add(txtCat, c);
		
//		btCat = new JButton("where...");
//		btCat.addActionListener(this);
//		catalogPane.add(btCat);
		
		c.gridx = 0;
		c.gridy = 2;

		catChooserPane = new EDCDirChooserPanel(
				"Where: ", "Choose directory");
		catChooserPane.getTxtFolderName().setText(f.getParentFile().getPath());
		catalogPane.add(catChooserPane, c);
		
		getContentPane().add(catalogPane, BorderLayout.CENTER);
		
		JPanel p = new JPanel();
		p.add(Box.createHorizontalStrut(5));
		
		btCancel = new JButton("Cancel");
		btCancel.addActionListener(this);
		p.add(btCancel);

		btOk = new JButton("OK");
		btOk.addActionListener(this);
		p.add(btOk);
		
		getContentPane().add(p, BorderLayout.PAGE_END);
		
	}

	public EDCDirChooserPanel getDirChooser() {
		return dirChooserPane;
	}

	public void setDirChooser(EDCDirChooserPanel dirChooser) {
		this.dirChooserPane = dirChooser;
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object src = e.getSource();
		if(src instanceof JButton) {
			JButton b = (JButton)src;
			
			if(b.getText().equals("OK")) {
				System.out.println(b.getText());
				//change DTD catalog file.
				EDCResource.URL_CATALOG_PATH = 
					catChooserPane.getTxtFolderName().getText()
					+File.separator+txtCat.getText();
				System.out.println("create file: "+ EDCResource.URL_CATALOG_PATH);
				//change DTD folder
//				EDCResource.dtdFolderPath = dirChooserPane.getTxtFolderName().getText();
				
				try {
					EDCResource.setInput(dirChooserPane.getTxtFolderName().getText());
					XMLControler.initXMLFile(EDCResource.URL_CATALOG_PATH);
				} catch (Exception e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				
				//refresh Navigator
				EDC.getCurrent().getNavigator().resetTree();
				try {
					EDC.getCurrent().getNavigator().createNodes(
							EDC.getCurrent().getNavigator().getRoot(),
							EDCResource.inputFile.getCanonicalPath());
				} catch (Exception ex){
					ex.printStackTrace();
				}
				EDC.getCurrent().getNavigator().getTree().updateUI();
				
				//Close internal frame
				try {
					setClosed(true);
				} catch (PropertyVetoException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
			else if(b.getText().equals("Cancel")) {
				System.out.println(b.getText());
				EDCResource.MANAGER_VIEW_REQUIRED = false;
				//Close internal frame
				try {
					setClosed(true);
				} catch (PropertyVetoException e1) {
					e1.printStackTrace();
				}
			}
			else if(b.getText().equals("where...")) {
				System.out.println(b.getText());
				
			}
		}
	}

}
